﻿Imports System
Imports System.IO
Imports System.Data.OleDb
Imports System.Data
Imports System.Drawing
Imports System.Drawing.Imaging
Namespace Arwani
    Partial Class Backend_AddNewSlider
        Inherits System.Web.UI.Page

        Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
            If Request.QueryString("divid") <> "" Then
                Dim strQ As String
                Dim arwCon As New ArwaniController

                If Me.fileImage.PostedFile.FileName <> "" Then
                    Dim ext As String = IO.Path.GetExtension(Me.fileImage.PostedFile.FileName)
                    Dim strFileName As String = Guid.NewGuid.ToString() & ext
                    Me.fileImage.PostedFile.SaveAs(Server.MapPath("../Slider/" & strFileName))
                    Call ResizeImages(strFileName, "Thumb_" & strFileName)
                    strQ = "insert into division_slider (title,description,banner,divisionid) values ('','','" & strFileName & "'," & CInt(Request.QueryString("divid")) & ")"
                    '" & Me.txtTitle.Text.Replace("'", "''") & "
                    arwCon.ExecuteNonQuery(strQ)
                End If
               
            End If
            Response.Redirect("AddNewSlider.aspx?divid=" & CInt(Request.QueryString("divid")))
        End Sub
        Public Function getImage(ByVal imgSrc As String) As String
            Dim strReturn As String = "images/main_banner.png"
            If File.Exists(Server.MapPath("../Slider/" & imgSrc)) Then
                strReturn = "../Slider/" & imgSrc
            End If
            Return strReturn
        End Function
        Protected Sub cmdBack_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdBack.Click
            Response.Redirect("ManageDivision.aspx")
        End Sub
        Sub ResizeImages(ByVal FileName, ByVal NewFileName)
            Dim intWidth, intHeight As Integer

            intWidth = 971 '*** Fix Width ***'  
            intHeight = 396   '*** If = 0 Auto Re-Cal Size ***'  

            Dim objGraphic As System.Drawing.Image = System.Drawing.Image.FromFile(Server.MapPath("../Slider/" & FileName))

            Dim objBitmap As Bitmap
            '*** Calculate Height ***'  
            If intHeight > 0 Then
                objBitmap = New Bitmap(objGraphic, intWidth, intHeight)
            Else
                If objGraphic.Width > intWidth Then
                    Dim ratio As Double = objGraphic.Height / objGraphic.Width
                    intHeight = ratio * intWidth
                    objBitmap = New Bitmap(objGraphic, intWidth, intHeight)
                Else
                    objBitmap = New Bitmap(objGraphic)
                End If
            End If

            '*** Save As  ***'  
            objBitmap.Save(Server.MapPath("../Slider/" & NewFileName), objGraphic.RawFormat)

            '*** Close ***'  
            objGraphic.Dispose()

            '*** Nothing ***'  
            objBitmap = Nothing
            objGraphic = Nothing

        End Sub

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If (Session("ADMIN_USER") IsNot Nothing) Then

                If Page.IsPostBack Then Exit Sub
                Call loadData()
            Else
                Response.Redirect("Default.aspx")
            End If

        End Sub
        Public Sub loadData()
            Dim arwCon As New ArwaniController
            Dim strQ As String = "select * from division_slider where divisionid=" & CInt(Request.QueryString("divid"))
            Dim dtTable As DataTable = arwCon.ExecuteQuery(strQ)
            Me.dtSliderImages.DataSource = dtTable
            Me.dtSliderImages.DataBind()

        End Sub

        Protected Sub dtSliderImages_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles dtSliderImages.ItemCommand
            If e.CommandName = "Delete" Then
                Dim arwCon As New ArwaniController
                Dim strQ As String = ""
                strQ = "select * from division_slider where id= " & CInt(e.CommandArgument)
                Dim dtTable As DataTable = arwCon.ExecuteQuery(strQ)
                If dtTable.Rows.Count > 0 Then
                    Try
 
                        If File.Exists(Server.MapPath("../Slider/" & dtTable.Rows(0)("banner"))) Then
                            File.Delete(Server.MapPath("../Slider/Thumb_" & dtTable.Rows(0)("banner")))
                        End If
                    Catch ex As Exception

                    End Try
                End If

                strQ = "delete from division_slider where id= " & CInt(e.CommandArgument)
                arwCon.ExecuteNonQuery(strQ)
                Call loadData()
            End If
        End Sub

        Protected Sub dtSliderImages_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles dtSliderImages.ItemDataBound
            Try
                Dim lnkDelete As LinkButton = CType(e.Item.FindControl("lnkDelete"), LinkButton)
                lnkDelete.Attributes.Add("onclick", "return confirm('Confirm delete this Banner?')")

                Dim ID As Integer = dtSliderImages.DataKeys(e.Item.ItemIndex)
                Dim txtSortOrder As TextBox = CType(e.Item.FindControl("txtSortOrder"), TextBox)
                txtSortOrder.Attributes.Add("onblur", "postSortOrder(this.value,'division_slider', 'id','" & ID & "')")
            Catch ex As Exception

            End Try
        End Sub
    End Class
End Namespace